<?php

namespace app\common\service;

use app\common\model\UserExtend;
use think\db\exception\DbException;
use think\facade\Db;

class UserExtendService
{
    /**
     * saveDataByField
     * @param $uid
     * @param $field
     * @param $value
     * @return void
     * @throws DbException
     */
    public function saveDataByField($uid, $field, $value)
    {
        // 保存条件
        $where = [
            ['uid', '=', $uid]
        ];

        // 保存数据
        $data = [
            'uid' => $uid,
            $field => $value,
            'update_time' => time(),
        ];

        // 查询数据
        $this->saveData($data, $where);
    }

    /**
     * saveData
     * @param array $data
     * @param array $where
     * @return void
     * @throws DbException
     */
    public function saveData(array $data, array $where = [])
    {
        $count = app(UserExtend::class)->where($where)->count();
        if ($count) {
            app(UserExtend::class)->where($where)->update($data);
        } else {
            app(UserExtend::class)->insert($data);
        }
    }
}